package com.open.party.cloud.view.examine.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.ExamineAttendanceCourseEntity;
import com.sinothk.android.utils.XUtils;
import com.sinothk.android.utils.inters.OnSuperListener;

import java.util.ArrayList;
import java.util.List;

public class ExamineAttendanceCourseListAdapter extends RecyclerView.Adapter<ExamineAttendanceCourseListAdapter.ViewHolder> {

    // 展示数据
    protected ArrayList<ExamineAttendanceCourseEntity> mData;
    private Context mContext;
    private OnSuperListener<ExamineAttendanceCourseEntity> onSuperListener;

    public ExamineAttendanceCourseListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<ExamineAttendanceCourseEntity> onSuperListener) {
        this.onSuperListener = onSuperListener;
    }

    public void setData(List<ExamineAttendanceCourseEntity> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<ExamineAttendanceCourseEntity> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.examine_class_attendance_course_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final ExamineAttendanceCourseEntity itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getCourseName()));
//
//        holder.teacherTv.setText("培训老师：" + XUtils.string().getNotNullValue(itemData.getUserName()));

        String startStr = "";
        if (itemData.getStartTime() != null) {
            startStr = XUtils.date().getDateStrByDate(itemData.getStartTime(), "yyyy-MM-dd HH:mm");
        }
        String endStr = "";
        if (itemData.getEndTime() != null) {
            endStr = XUtils.date().getDateStrByDate(itemData.getEndTime(), "yyyy-MM-dd HH:mm");
            if (XUtils.string().isNotEmpty(startStr)) {
                endStr = "-" + endStr;
            }
        }
        holder.timeTv.setText("培训时间：" + startStr + endStr);

        if (mData.size() - 1 == position) {
            holder.bottomLineTv.setVisibility(View.VISIBLE);
        } else {
            holder.bottomLineTv.setVisibility(View.GONE);
        }

        if (itemData.getAttence() != null && itemData.getAttence().getCreateTime()!= null) {
            holder.attendanceTimeTv.setText("打卡时间：" + XUtils.date().getDateStrByDate(itemData.getAttence().getCreateTime(),"yyyy-MM-dd HH:mm"));

            holder.rootView.setClickable(false);
            holder.rootView.setBackgroundResource(R.drawable.selector_bg_green_square);
        } else {
            holder.attendanceTimeTv.setText("打卡时间：");

            if (itemData.isHasSelected()) {
                holder.rootView.setBackgroundResource(R.drawable.selector_bg_red_square);
            }else{
                holder.rootView.setBackgroundResource(R.drawable.selector_bg_white_square);
            }

            if (onSuperListener != null) {
                holder.rootView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onSuperListener.onClick(position, itemData, "rootView");
                    }
                });
            }
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rootView;
        TextView titleTv, teacherTvs, timeTv, attendanceTimeTv, bottomLineTv;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
//            teacherTv = itemView.findViewById(R.id.teacherTv);
            timeTv = itemView.findViewById(R.id.timeTv);
            attendanceTimeTv = itemView.findViewById(R.id.attendanceTimeTv);

            bottomLineTv = itemView.findViewById(R.id.bottomLineTv);
        }
    }
}